package cst.cn.edu.neusoft.xumingze224.yuejian_5.Model;

import java.util.List;


import cst.cn.edu.neusoft.xumingze224.yuejian_5.Listener.ListListener;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Listener.TListener;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Service.Server;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Service.TService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitBaseModel {
    Retrofit retrofit;
    TService tService;
    public Retrofit getRetrofit(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Server.BASE_URL)//主机地址
                .addConverterFactory(GsonConverterFactory.create())//数据转换方法
                .build();
        return retrofit;
    }
    public TService gettService(){
        tService =retrofit.create(TService.class);
        return tService;
    }
    public <T> void callenqueue(Call<T> call, final TListener<T> tListener){
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    tListener.onResponse(response.body());
                }
                else
                    tListener.onFail("用户不存在");
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                tListener.onFail(t.toString());
            }
        });
    }
    public <T> void callenqueueList(Call<List<T>> call, final ListListener<T> listListener){
        call.enqueue(new Callback<List<T>>() {
            @Override
            public void onResponse(Call<List<T>> call, Response<List<T>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listListener.onResponse(response.body());
                }
                else
                    listListener.onFail("用户不存在");
            }


            @Override
            public void onFailure(Call<List<T>> call, Throwable t) {
                listListener.onFail(t.toString());

            }
        });
    }
}
